package com.lk.springboot.service.impl;

import com.lk.springboot.dao.ScheduleMapper;
import com.lk.springboot.service.ScheduleService;
import com.lk.springboot.utils.RandomUtil;
import com.lk.springboot.vo.ScruIdAndType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * @author lk
 * @date 2020/1/20 0020 18:34
 */
@Service
public class ScheduleServiceImpl implements ScheduleService {

    @Autowired
    ScheduleMapper scheduleMapper;

    private final int TM = 20; // 单日里程任务要求
    private final int MIN_MILEAGE = 0; // 模拟单次最低里程
    private final int MAX_MILEAGE = 15; // 模拟单次最高里程

    @Override
    public void clearToken() {
        scheduleMapper.clearToken();
    }

    @Override
    public void resetSpeed() {
        scheduleMapper.resetUserTaskSpeed();
    }

    @Override
    public void resetMarketNum() {
        scheduleMapper.resetMagicStatus();
        scheduleMapper.resetMarketNum();
    }

    @Override
    public void resetIsBought() {
        scheduleMapper.resetIsBought();
    }

    @Override
    public void updateScoreExchange() {
        ArrayList<ScruIdAndType> scRuIdTypeList = scheduleMapper.getScoreRu();// 所有规则的列表《scru_id，ru_type》
        int scoreCenterNum = 0;
        int magicMarketNum = 0;
        for (ScruIdAndType scruIdAndType : scRuIdTypeList) {
            if (scruIdAndType.getType().equals("score"))
                scoreCenterNum++; // 增加兑换中心的 规则条数
//            else
//                magicMarketNum++;// 增加魔法市场的 规则条数
        }
        RandomUtil randomUtil = new RandomUtil();

        List<String> pIdList = randomUtil.randomGetPid(10045, 10001, scoreCenterNum + magicMarketNum);// p_id列表

        List<String> scoreRareList = randomUtil.scoreCenterRandomGetRares(0, scoreCenterNum);// 兑换中心的 稀有度列表
//        List<String> magicRareList = randomUtil.scoreCenterRandomGetRares(1, magicMarketNum);// 魔法市场的 稀有度列表
        ArrayList<String> spIdList = new ArrayList<>();// sp_id列表
        for (int i = 0; i < scoreCenterNum; i++) {
            spIdList.add(scheduleMapper.getSPList(pIdList.get(i), scoreRareList.get(i)));
        }
//        for (int i = 0; i < magicMarketNum; i++) {
//            spIdList.add(scheduleMapper.getSPList(pIdList.get(i + scoreCenterNum), magicRareList.get(i)));
//        }
        for (int i = 0; i < scoreCenterNum + magicMarketNum; i++) {
            scheduleMapper.updateScoreRuSP(scRuIdTypeList.get(i).getScru_id(), spIdList.get(i));
        }
    }

    @Override
    public void resetTodayMileage() {
        scheduleMapper.resetTodayMileage();
    }

    @Override
    public void addTodayMileage() {
        // 模拟今日的一个单次里程（随机里程数）
        int mileage = (int) (Math.random() * (MAX_MILEAGE - MIN_MILEAGE + 1)) + MIN_MILEAGE;
        // 模拟，随机选取一个用户
        ArrayList<String> UIdList = scheduleMapper.getAllUserId();
        String u_id = UIdList.get(new Random().nextInt(UIdList.size()));
        // 将单次里程计入今日里程
        scheduleMapper.addTodayMileage(u_id,mileage);
        // 将单次里程计入累计里程
        scheduleMapper.addTotalMileage(u_id,mileage);
        // 获取今日里程数，并判断 里程任务 的进度是否需要将进度置为1
        int myTodayMileage = scheduleMapper.getTodayMileage(u_id);
        if(myTodayMileage >= TM){// 里程数达到，里程任务进度可以置为1
            scheduleMapper.signMileage(u_id);
        }
        // 标记乘地铁任务进度为1
        if (myTodayMileage > 0){
            scheduleMapper.signBySubway(u_id);
        }
    }
}
